package com.lingshi.shopping_user_customer_api.controller;

import cn.hutool.core.util.RandomUtil;
import com.lingshi.shopping.entity.ShoppingUser;
import com.lingshi.shopping.result.BaseResult;
import com.lingshi.shopping.service.MessageService;
import com.lingshi.shopping.service.ShoppingUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

/**
 * 用户模块
 */
@RestController
@RequestMapping("/user/shoppingUser")
public class ShoppingUserController {

    @DubboReference
    private MessageService messageService;

    @DubboReference
    private ShoppingUserService shoppingUserService;

    /**
     * 注册发送短信
     * @param phone 手机号
     * @return
     */
    @GetMapping("/sendMessage")
    public BaseResult sendMessage(String phone){

        String randomCode = RandomUtil.randomNumbers(4);
        System.out.println("randomCode = " + randomCode);

        //发送短信
        messageService.sendMessage(phone,randomCode);

        //保存redis
        shoppingUserService.saveRegisterCheckCode(phone,randomCode);

        return BaseResult.success();
    }

    /**
     * 检查注册用户手机验证码
     * @param phone 手机号
     * @param checkCode 验证码
     * @return
     */
    @GetMapping("/registerCheckCode")
    public BaseResult registerCheckCode(String phone,String checkCode){
        shoppingUserService.registerCheckCode(phone,checkCode);
        return BaseResult.success();
    }

    /**
     * 注册用户
     * @param shoppingUser
     * @return
     */
    @PostMapping("/register")
    public BaseResult register(@RequestBody ShoppingUser shoppingUser){
        shoppingUserService.register(shoppingUser);
        return BaseResult.success();
    }

    /**
     * 账号密码登录
     * @param shoppingUser
     * @return
     */
    @PostMapping("/loginPassword")
    public BaseResult loginPassword(@RequestBody ShoppingUser shoppingUser){
        String token = shoppingUserService.loginPassword(shoppingUser.getUsername(), shoppingUser.getPassword());
        return BaseResult.success(token);
    }

    /**
     * 发送登录验证码
     * @param phone 手机
     * @return
     */
    @GetMapping("/sendLoginCheckCode")
    public BaseResult sendLoginCheckCode(String phone){
        String randomCode = RandomUtil.randomNumbers(4);
        //发送短信
        messageService.sendMessage(phone,randomCode);
        //保存验证码
        shoppingUserService.saveLoginCheckCode(phone,randomCode);
        return BaseResult.success();
    }

    /**
     * 验证码登录
     * @param phone 手机号
     * @param checkCode 验证码
     * @return
     */
    @PostMapping("/loginCheckCode")
    public BaseResult loginCheckCode(String phone,String checkCode){
        String token = shoppingUserService.loginCheckCode(phone, checkCode);
        return BaseResult.success(token);
    }

    /**
     * 获取登录后的账号
     * @param username
     * @return
     */
    @GetMapping("/getName")
    public BaseResult getName(@RequestHeader("username") String username){
        return BaseResult.success(username);
    }


}
